package com.mengfou;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @author 梦否
 * @date 2025/07/05 14:27
 */
public class ReflectUtils {

    public Object getField(Object proxy, String name) {
        try {
            Field declaredField = proxy.getClass().getDeclaredField(name);
            declaredField.setAccessible(true);
            return declaredField.get(proxy);
        } catch (NoSuchFieldException | IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }

    public void invokeMethod(Object messages, String clear) {
        try {
            Method declaredMethod = messages.getClass().getDeclaredMethod(clear);
            declaredMethod.setAccessible(true);
            declaredMethod.invoke(messages);
        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    public Object newInstance(String className, ClassLoader classLoader) {
        Object o = null;
        try {
            Class<?> aClass = Class.forName(className, true, classLoader);
            o = aClass.newInstance();
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
            e.printStackTrace();
        }
        return o;
    }
}
